import type { Effect, Reducer } from 'umi';
import { queryBanners } from './service';

import type { SrcList } from './data.d';

export interface StateType {
    list: SrcList[]
}

export interface ModelType {
    namespace: string;
    state: StateType;
    effects: {
        fetch: Effect;
    }
    reducers: {
        queryBanners: Reducer<StateType>;
    }
}

const Model: ModelType =  {
    namespace: 'bannermodel',

    state: {
        list: []
    },

    effects: {
        *fetch({ payload }, { call, put }) {
            const response = yield call(queryBanners, payload)
            yield put({
                type: 'queryBanners',
                payload: Array.isArray(response) ? response : []
            })
        }
    },

    reducers: {
        queryBanners(state, action) {
            return {
                ...state,
                list: action.payload
            }
        }
    }
}

export default Model;